﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using TimKen.App;
using TimKen.DataEntity;
using TimKen.BussLogic;
using System.Collections.Generic;
namespace TimKen.Web.Admin.PAttribute
{
    public partial class POption_Add : TimKen.App.AdminPage
    {
        POptionManager pom = new POptionManager();

        /// <summary>
        /// 分类ID
        /// </summary>
        public int CategoryID {
            get {
                if (ViewState["CategoryID"] != null)
                    return Convert.ToInt32(ViewState["CategoryID"]);
                else
                    return 0;
            }
            set {
                ViewState["CategoryID"] = value;
            }
        }
        public override string ActionCode
        {
            get
            {
                return "25";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                rblFieldType_Load();
                txtFieldMetaDataCN.Enabled = false;
                txtFieldMetaDataEN.Enabled = false;

                if (Request.QueryString["CategoryID"] != null)
                {
                    CategoryID = TypeParse.StrToInt(Request.QueryString["CategoryID"], 0);
                    this.hlTypeOptionList.Visible = true;
                    this.hlTypeOptionList.NavigateUrl = "CategoryOption_List.aspx?CategoryID=" + CategoryID;
                    this.panelSearchAble.Visible = true;
                }

                else if (Request.QueryString["OptionId"] != null)
                {
                    OptionID = TypeParse.StrToInt(Request.QueryString["OptionId"], 0);
                    this.hlOptionManage.Visible = true;
                    BindData();
                }

                else
                {

                    this.hlOptionManage.Visible = true;
                }

              
                BindResources();
          
            }
            for (int i = 0; i < rblFieldType.Items.Count; i++)
            {
                ListItem item = rblFieldType.Items[i];
                item.Attributes.Add("onclick", "radioClicked(" + item.Value + ")");
            }
        }

        public int OptionID
        {
            get
            {
                if (ViewState["OptionID"] != null && TypeParse.IsNumeric(ViewState["OptionID"]))
                    return Convert.ToInt32(ViewState["OptionID"]);
                else
                    return 0;
            }
            set
            {
                ViewState["OptionID"] = value;
            }
        }

        /// <summary>
        ///  绑定属性列表数据
        /// </summary>
        protected void BindData()
        {
            if (OptionID != 0)
            {
                POption optionCn = pom.GetModel(OptionID, LanguageEnum.Chinese);
                POption optionEn = pom.GetModel(OptionID, LanguageEnum.English);

                this.txtAttrNameCN.Text = optionCn.OptionName;
                this.txtAttrNameEN.Text = optionEn.OptionName;
                this.rblFieldType.SelectedValue = optionCn.FieldType.ToString();
                this.txtFieldMetaDataCN.Text = optionCn.FieldMetaData;
                this.txtFieldMetaDataEN.Text = optionEn.FieldMetaData;
                txtFieldMetaDataCN.Enabled = (optionCn.FieldType == 2 ? true : false);
                txtFieldMetaDataEN.Enabled = (optionCn.FieldType == 2 ? true : false);
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            POption opcn = new POption();
            POption open = new POption();

            opcn.OptionName = txtAttrNameCN.Text;
            open.OptionName = txtAttrNameEN.Text;

            opcn.LanguageID = (int)LanguageEnum.Chinese;
            open.LanguageID = (int)LanguageEnum.English;

            opcn.FieldType = Convert.ToInt32(rblFieldType.SelectedValue);
            open.FieldType = Convert.ToInt32(rblFieldType.SelectedValue);

            opcn.FieldMetaData = txtFieldMetaDataCN.Text;
            open.FieldMetaData = txtFieldMetaDataEN.Text;
            IList<POption> list = new List<POption> { opcn, open };
            bool actionSuccess = false;
            string alertMessage = GetLangString("SaveSuccessful");
            //添加
            if (OptionID == 0)
            {
                int optionid = pom.Add(list);

                if (CategoryID != 0)
                {
                    PCategoryManager CategoryManager = new PCategoryManager();

                    bool isSearchAble = false;

                    if (this.cbAttributeIndex.Checked)
                        isSearchAble = true;
                    CategoryManager.AddOptionToCategory(optionid, CategoryID,isSearchAble);
                }
                if (optionid > 0)
                    actionSuccess = true;
            }
            //编辑
            else
            {
                opcn.OptionID = OptionID;
                open.OptionID = OptionID;
                list[0] = opcn;
                list[1] = open;
                actionSuccess = pom.Update(list);
            }
            alertMessage = pom.ValidMessage;
            if (actionSuccess)
            {
                txtAttrNameCN.Text = "";
                txtAttrNameEN.Text = "";
                txtFieldMetaDataCN.Text = "";
                txtFieldMetaDataEN.Text = "";
                rblFieldType.SelectedIndex = 0;
                BindData();
            }
            Alert(alertMessage);
        }

        public void BindResources()
        {
            btnOK.Text = GetLangString("OK");
            btnCancel.Text = GetLangString("Cancel");
        }

        protected void rblFieldType_Load()
        {
            ListItem items0 = new ListItem(GetLangString("FieldType_TextBox"), "0");
            ListItem items1 = new ListItem(GetLangString("FieldType_TextArea"), "1");
            ListItem items2 = new ListItem(GetLangString("FieldType_Select") + GetLangString("FieldType_Select_Tip"), "2");
            items0.Selected = true;
            rblFieldType.Items.Add(items0);
            rblFieldType.Items.Add(items1);
            rblFieldType.Items.Add(items2);
        }
    }
}
